<?php defined('SYSPATH') or die('No direct script access.');

class Model_Services_Habit extends Model{
	
	public $habit_patient = NULL;

	public function __construct($habit_id=false){
		if ($habit_id) $this->habit_patient = $this->get($habit_id);
	}
	
	public function newHabit($patient_id){
		$newHabit = ORM::factory("dao_habits");
		$newHabit->smoke = 0; //tinyint
		$newHabit->smoked = 0; //tinyint
		$newHabit->years_old_at_smooke_start = 0; //int
		$newHabit->years_old_at_smooke_finish = 0; //int
		$newHabit->cigarettes_per_day = 0; //int
		$newHabit->automedication = 0; //tinyint
		$newHabit->angry_level = 0; //int
		$newHabit->stress_level = 0;//int
		$newHabit->blocks_walked_per_day = 0; //int
		$newHabit->consume_alcohol = 0;//tinyint
		$newHabit->sedentary = 0;//tinyint
		$newHabit->work_place = ""; //string
		$newHabit->patient_id = $patient_id; //int
		$this->habit_patient = $newHabit;
		return;
	}
	
	
	public function get($id=false){
		if ($id) return ORM::factory("dao_habits")->find($id);
		return $this->habit_patient;
	}

	public function getId(){
		return ($this->habit_patient) ? $this->habit_patient->id : 0;
	}
		
	public function autoMedicates(){
		return ($this->habit_patient) ? $this->habit_patient->automedication : 0;
	}
	
	public function blocksPerDay(){
		return ($this->habit_patient) ? $this->habit_patient->blocks_walked_per_day : false;
	}
	
	public function cigarettesPerDay(){
		return ($this->habit_patient) ? $this->habit_patient->cigarettes_per_day : false;
	}
	
	public function consumesAlcohol(){
		return ($this->habit_patient) ? $this->habit_patient->consume_alcohol : 0;
	}
		
	public function endAgeSmoking(){
		return ($this->habit_patient) ? $this->habit_patient->years_old_at_smooke_finish : false;
	}
	
	public function getAngryLevel(){
		return ($this->habit_patient) ? $this->habit_patient->angry_level : 0;
	}
	
	public function getStressLevel(){
		return ($this->habit_patient) ? $this->habit_patient->stress_level : 0;
	}
	
	public function hasSmoked(){
		return ($this->habit_patient) ? $this->habit_patient->smoked : 0;
	}
	
	public function isSedentary(){
		return ($this->habit_patient) ? $this->habit_patient->sedentary : 0;
	}
	
	public function smokes(){
		return ($this->habit_patient) ? $this->habit_patient->smoke : 0;
	}
	
	public function startAgeSmoking(){
		return ($this->habit_patient) ? $this->habit_patient->years_old_at_smooke_start : false;
	}
	
	public function workplace(){
		return ($this->habit_patient) ? $this->habit_patient->work_place : false;
	}
	
	public function currentlySmoking(){
		$smokes = $this->smokes();
		$smoked = $this->hasSmoked();
		//if (($smokes == "") && ($smoked == "")) return 0;
		return (($smokes == 1) && ($smoked == 0));
	}
	
	public function neverSmoked(){
		$smokes = $this->smokes();
		$smoked = $this->hasSmoked();
		//if (($smokes == "") && ($smoked == "")) return false;
		return (($smokes == 0) && ($smoked == 0));
	}
	
	public function hasQuitSmoking(){
		$smokes = $this->smokes();
		$smoked = $this->hasSmoked();
		//if (($smokes == "") && ($smoked == "")) return false;
		return (($smokes == 0) && ($smoked == 1));
	}
	
	
		
	public function save($id, array $data){
		Fire::log($data);
				
		$this->habit_patient = ($id > 0) ? ORM::factory("dao_habits",$id) : ORM::factory("dao_habits");
		
		$this->habit_patient->smoke = $data['smoke']; //int
		$this->habit_patient->smoked = $data['smoked']; //int
		$this->habit_patient->years_old_at_smooke_start = $data['years_old_at_smooke_start']; //int
		$this->habit_patient->years_old_at_smooke_finish = $data['years_old_at_smooke_finish']; //int
		$this->habit_patient->cigarettes_per_day = $data['cigarettes_per_day']; //int
		$this->habit_patient->automedication = (isset($data['automedication'])) ? $data['automedication'] : 0; //int
		$this->habit_patient->angry_level = $data['angry_level']; //int
		$this->habit_patient->stress_level = $data['stress_level'];//int
		$this->habit_patient->blocks_walked_per_day = $data['blocks_walked_per_day']; //int
		$this->habit_patient->consume_alcohol = (isset($data['consume_alcohol'])) ? $data['consume_alcohol'] : 0;//int
		$this->habit_patient->sedentary = (isset($data['sedentary']))? $data['sedentary'] : 0;//int
		$this->habit_patient->work_place = $data['work_place']; //int
		$this->habit_patient->patient_id = $data['patient_id']; //int
				
		$this->habit_patient->save();
		
		$this->habit_patient->patient->habits_id = $this->habit_patient->id; //Esto es horrible, habra que cambiarlo
		$this->habit_patient->patient->save();
				
		return $this->habit_patient->id;
	}
	
	public function delete($id){
		$this->habit_patient = ORM::factory("dao_habits",$id);
		$this->habit_patient->delete();
		return "Habito: ".$id." eliminado con exito";
	}
	
	/**
	 * 
	 * return patient habits
	 * @param int $id
	 */
	public function getPatientHabits($id){
		return ORM::factory("dao_habits")->where("patient_id","=",$id)->find();
	}
}
?>
